﻿/*
创建人：zhangrj
日期：2022-04-15
描述：批次暂停、释放页面js
*/
var page = {
    stopReason: [{ reason: "产品工艺变更暂停"}], //暂停批次原因
    releaseReason: [{ reason: "产品工艺变更释放"}], //释放批次原因
    pagetotal: 0,
    pageSize: 10,
    pageNumber: 1,
    url: getRootPath() + "/Ashx/SuspendContainerManageHandler.ashx", //后台处理逻辑url
    init: function () {
        customizedEasyuiPagination('pp'); //定制化分页
        page.bind();
        page.initData();
    },
    bind: function () {
        //批次主列表
        $("#gdContainerInfo").datagrid({
            onLoadSuccess: function (data) {
                $('#pp').pagination('refresh', {	// 改变选项，并刷新分页栏信息
                    total: page.pagetotal,
                    pageSize: page.pageSize,
                    pageNumber: page.pageNumber
                });
                customizedEasyuiPagination('pp'); //定制化分页
            },
            columns: [[
                 { field: 'ck', title: '选择', checkbox: true },
                 { field: 'MFGORDERNAME', title: '订单号', width: 120 },
		         { field: 'PRODUCTDESC', title: '零件名称', width: 150 },
                 { field: 'PRODUCTREVISION', title: '零件版本', width: 60 },
                 { field: 'WORKFLOWNAME', title: '工艺名称', width: 150 },
                 { field: 'WORKFLOWREVISION', title: '工艺版本', width: 60 },
                 { field: 'PRODUCTBATCH', title: '件号/批次号', width: 180 },
                 { field: 'WORKFLOWSTEPNAME', title: '当前序', width: 180 },
                 { field: 'CONTAINERTYPENAME', title: '批次类型', width: 120 },
                 { field: 'FROMWORKCENTERNAME', title: '主制工区', width: 130 },
                 { field: 'TOTALQTY', title: '在制总数', width: 80 },
                 { field: 'ISHOLD', title: '暂停', width: 50 },
                 { field: 'CONTAINERTYPE', hidden: true },
                 { field: 'TOGETHERFLAG', hidden: true },
                 { field: 'CONTAINERID', hidden: true },
                 { field: 'WORKFLOWSTEPID', hidden: true },
                 { field: 'WORKFLOWID', hidden: true },
                 { field: 'PRODUCTID', hidden: true },
                 { field: 'PRODUCTNAME', hidden: true },
                 { field: 'MFGORDERID', hidden: true }
                ]],
            onCheck: function (index, rowData) {
                var checkedData = $("#gdContainerInfo").datagrid("getChecked");
                if (checkedData.length > 0) {
                    page.searchDataDetail(checkedData);
                }
            },
            onUncheck: function (index, rowData) {
                var checkedData = $("#gdContainerInfo").datagrid("getChecked");
                if (checkedData.length > 0) {
                    page.searchDataDetail(checkedData);
                } else {
                    clearEasyuiDataGrid("gdContainerInfo2", 1);
                }
            },
            onCheckAll: function (index, rowData) {
                var checkedData = $("#gdContainerInfo").datagrid("getChecked");
                if (checkedData.length > 0) {
                    page.searchDataDetail(checkedData);
                }
            },
            onUncheckAll: function (index, rowData) {
                var checkedData = $("#gdContainerInfo").datagrid("getChecked");
                if (checkedData.length > 0) {
                    page.searchDataDetail(checkedData);
                } else {
                    clearEasyuiDataGrid("gdContainerInfo2", 1);
                }
            }
        });
        //批次号列表加载
        $("#gdContainerInfo2").datagrid({
            columns: [[
                 { field: 'ck', title: '选择', checkbox: true },
		         { field: 'CONTAINERNAME', title: '流水卡号', width: 200 },
                 { field: 'QTY', title: '在制数', width: 60 },
                 { field: 'CREATEDATE', title: '暂停时间', width: 120 },
                 { field: 'CONTAINERID', title: '批次ID', hidden: true },
                 { field: 'WORKFLOWID', title: '工艺ID', hidden: true },
                 { field: 'MFGORDERNAME', title: '订单ID', hidden: true },
                 { field: 'PRODUCTID', title: '件号ID', hidden: true },
                 { field: 'BOMID', hidden: true }
	           ]],
            onClickRow: function (rowIndex, rowData) {
                switch (page.pageType) {
                    case "2":
                        stepAttr.bindDatas(rowData);
                        break;
                    case "3":
                        containerAttr.bindDatas(rowData);
                        break;
                    case "4":
                        accessoriesAttr.bindDatas(rowData);
                        break;
                    default:
                        break;
                }
            },
            singleSelect: true
        });
        //分页事件
        $('#pp').pagination({
            onSelectPage: function (pageNumber, pageSize) {
                customizedEasyuiPagination('pp'); //定制化分页
                page.pageSize = pageSize; page.pageNumber = pageNumber;
                page.search();
            }
        });
        //查询
        $("#btnSearch").on("click", function () {
            page.pageNumber = 1;
            page.search();
        });
        //暂停
        $('#btnFreeze').on("click", function () {
            page.saveData("1");
        });
        //释放
        $('#btnRelease').on("click", function () {
            page.saveData("0");
        });
        $(document).ready(function () { //页面加载完成才绑定下拉，否则会报错
            $('#selStatus').combobox({
                onSelect: function (record) {
                    if (record.value === "0") {
                        $("#divFreeze").show();
                        $("#divRelease").hide();
                        //绑定暂停原因数据
                        $("#selFreezeReason").combobox({
                            valueField: 'reason',
                            textField: 'reason',
                            panelHeight: 200,
                            data: page.stopReason, //下拉数据
                            value: page.stopReason[0].reason
                        });
                    } else {
                        $("#divFreeze").hide();
                        $("#divRelease").show();
                        //绑定释放原因数据
                        $("#selReleaseReason").combobox({
                            valueField: 'reason',
                            textField: 'reason',
                            panelHeight: 200,
                            data: page.releaseReason, //下拉数据
                            value: page.releaseReason[0].reason
                        });
                    }
                }
            });
        });
    },
    //初始化页面数据
    initData: function () {
        $.ajax({
            url: page.url,
            type: 'Post',
            data: { action: "InitPageData" },
            dataType: "json",
            cashe: false,
            beforeSend: function () {
                $.messager.progress({ text: '', msg: '处理中...' });
            },
            success: function (result) {
                $.messager.progress('close');
                if (result.Result) {
                    //绑定工艺员数据
                    $("#selTechniqueEmployee").combobox({
                        valueField: 'EMPLOYEEID',
                        textField: 'FULLNAME',
                        panelHeight: 200,
                        data: result.DBTable, //下拉数据
                        value: result.DBTable[0].EMPLOYEEID
                    });
                }
                displayMessage(result.Message, result.Result);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $.messager.progress('close');
                displayMessage(XMLHttpRequest.responseText, false);
            }
        });
    },
    //查询
    search: function () {
        clearMessage();
        clearEasyuiDataGrid("gdContainerInfo", 1); //清空批次列表
        clearEasyuiDataGrid("gdContainerInfo2", 1); //清空批次详细列表
        var mfgOrderName = $.trim($('#txtMfgOrderName').textbox('getValue'));
        var productName = $.trim($('#txtProductName').textbox('getValue'));
        var containerName = $.trim($('#txtContainerName').textbox('getValue'));
        var techEmployeeId = $('#selTechniqueEmployee').combobox('getValue'); //工艺员
        var status = $('#selStatus').combobox('getValue');
        var reason = "";
        if (status === "1") { //查询已暂停，需限定原因
            for (var i = 0; i < page.stopReason.length; i++) {
                reason += "'" + page.stopReason[i].reason + "',";
            }
        }
        var queryData = {
            action: "GetContainerInfo",
            pageSize: page.pageSize,
            pageNumber: page.pageNumber,
            mfgOrderName: mfgOrderName,
            productName: productName,
            containerName: containerName,
            status: status,
            techEmployeeId: techEmployeeId,
            reason: reason
        };
        $.ajax({
            url: page.url,
            type: 'Post',
            data: queryData,
            dataType: "json",
            cashe: false,
            beforeSend: function () {
                $.messager.progress({ text: '', msg: '处理中...' });
            },
            success: function (result) {
                $.messager.progress('close');
                if (result.Result) {
                    //批次列表
                    page.pagetotal = result.RowCount;
                    $("#gdContainerInfo").datagrid('loadData', { total: result.RowCount, rows: result.DBTable });
                }
                displayMessage(result.Message, result.Result);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $.messager.progress('close');
                displayMessage(XMLHttpRequest.responseText, false);
            }
        });
    },
    //查询批次列表
    searchDataDetail: function (rowDatas) {
        clearMessage();
        var reason = "";
        for (var i = 0; i < page.stopReason.length; i++) {
            reason += "'" + page.stopReason[i].reason + "',";
        }
        clearEasyuiDataGrid("gdContainerInfo2", 1);
        var ajaxParam = { action: "GetContainerDetails", params: JSON.stringify(rowDatas), reason: reason };
        $.ajax({
            type: "Post",
            url: page.url,
            data: ajaxParam,
            cashe: false,
            dataType: "json",
            async: true,
            beforeSend: function () {
                $("#gdContainerInfo2").datagrid('loading');
            },
            success: function (data) {
                $("#gdContainerInfo2").datagrid('loaded');
                if (data.Result) {
                    $("#gdContainerInfo2").datagrid('loadData', { rows: data.DBTable });
                    //选择所有
                    $("#gdContainerInfo2").datagrid('checkAll');
                }
                displayMessage(data.Message, data.Result);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $("#gdContainerInfo2").datagrid('loaded');
                alert(XMLHttpRequest.responseText);
            }
        });
    },
    //暂停、释放批次
    saveData: function (type) {
        var datas = $("#gdContainerInfo2").datagrid('getChecked'); //获取所有详细批次勾选的行
        if (datas.length === 0) {
            displayMessage("未选择批次", false);
            return;
        }
        var row = $("#gdContainerInfo").datagrid('getChecked'); //获取所有批次勾选的行
        if (row.length === 0) {
            displayMessage("未选择主批次", false);
            return;
        }
        var reason = "";
        if (type === "1") {
            reason = $('#selFreezeReason').combobox("getText");
        } else {
            reason = $('#txtReleaseReason').textbox("getText");
        }
        var formdata = {
            action: "OperContainerStep",
            type: type, //1表示暂停,0表示释放
            productName: row[0].PRODUCTNAME,
            productId: row[0].PRODUCTID,
            reason: reason,
            params: JSON.stringify(datas)
        };
        $.ajax({
            type: "Post",
            url: page.url,
            data: formdata,
            cashe: false,
            dataType: "json",
            async: true,
            beforeSend: function () {
                $.messager.progress({ text: '', msg: '处理中...' });
            },
            success: function (data) {
                $.messager.progress('close');
                if (data.Result) {
                    clearEasyuiDataGrid("gdContainerInfo", 1);
                    clearEasyuiDataGrid("gdContainerInfo2", 1);
                }
                displayMessage(data.Message, data.Result);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $.messager.progress('close');
                alert(XMLHttpRequest.responseText);
            }
        });
    }

}
page.init();